<template>
  <el-dialog
    :title="dialogObj.title"
    append-to-body
    :visible.sync="dialogObj.show"
    :close-on-click-modal="false"
    width="500px"
  >
    <el-form
      ref="form"
      :model="form"
      status-icon
      label-width="140px"
      :rules="rules"
      :disabled="dialogObj.read"
      v-if="dialogObj.show"
    >
      <el-form-item label="特许分类名称：" prop="categoryName">
        <el-input
          v-model="form.categoryName"
          style="width:200px;"
          size="mini"
          :placeholder="placeholderTips.content"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="sub" size="mini">确 定</el-button>
      <el-button @click="dialogObj.show = false" size="mini">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { _debounce } from "@u/debounce";
import { placeholderTips, validateString2 } from "@u/validate";
import { AddProductcategoryApi, updateProductcategoryApi } from "@s/api";
export default {
  name: "dialogCom",
  props: ["dialogObj"],
  data() {
    return {
      placeholderTips: placeholderTips,
      form: {
        categoryName: ""
      },
      rules: {
        categoryName: [
          { required: true, message: "请输入内容", trigger: "blur" },
          { validator: validateString2 }
        ]
      }
    };
  },

  methods: {
    initDialog() {
      if (this.dialogObj.id) {
        this.form.categoryName = this.dialogObj.form.categoryName;
      } else {
        this.form.categoryName = "";
      }
    },
    sub: _debounce(function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.dialogObj.id) {
            let json = {
              categoryType: 2,
              categoryName: this.form.categoryName,
              id: this.dialogObj.id
            };
            updateProductcategoryApi(json).then(() => {
              this.dialogObj.show = false;
              this.$emit("reflash");
              this.$message({
                type: "success",
                message: "修改成功！"
              });
            });
          } else {
            let json = {
              categoryType: 2,
              categoryName: this.form.categoryName
            };
            AddProductcategoryApi(json).then(() => {
              this.dialogObj.show = false;
              this.$emit("reflash");
              this.$message({
                type: "success",
                message: "添加成功！"
              });
            });
          }
        }
      });
    })
  },
  watch: {
    "dialogObj.show"(val) {
      if (val) {
        this.initDialog();
      }
    }
  }
};
</script>
<style scoped lang="scss"></style>
